import { Result, ResultError } from "@/types";
import { Ref, ref, reactive, watch, getCurrentInstance } from "vue";
import { cancelUserOrder, receiptUserOrder, payUserOrder } from "@/api/user";
export default function (_call: any) {

    const { proxy } = getCurrentInstance() as any;
    const {
        proxy: { $utils },
    } = getCurrentInstance() as any;

    // 复制
    const _onCory = (orderNo: string) => {
        $utils.cory(orderNo)
    }

    const _onGoods = (id: number) => {
        $utils.routeRedirectTo("/pages/goods/detail", { id })
    }

    /**
    * 取消订单
    */
    const _cancelOrder = (id: number) => {
        uni.showModal({
            title: "友情提示",
            content: "确认要取消该订单吗？",
            success(o) {
                if (o.confirm) {
                    cancelUserOrder({
                        id
                    }).then((res: Result) => {
                        $utils.successMsg(res.message)
                        if (typeof _call == 'function') {
                            _call()
                        }
                    }).catch((err: ResultError) => {
                        $utils.errorMsg(err)
                    })
                }
            }
        })
    }

    const _toExpress = (id: number) => {

    }

    // 确定收货
    const _onReceipt = (id: number) => {
        uni.showModal({
            title: "友情提示",
            content: "您确定已收到货了吗？",
            success(o) {
                if (o.confirm) {
                    receiptUserOrder({
                        id
                    }).then((res: Result) => {
                        $utils.successMsg(res.message)
                        if (typeof _call == 'function') {
                            _call()
                        }
                    }).catch((err: ResultError) => {
                        $utils.errorMsg(err)
                    })
                }
            }
        })
    }

    // 去评价
    const _onComment = (id: number) => {
        $utils.routeRedirectTo('/otherpages/user/order/comment', {
            id
        });
    }


    // 支付方式选择
    const _onPay = (param: any) => {
        proxy?.$refs['payModalRef']?.open(param)
    }

    // 去支付
    const _toPay = (res: any) => {
        let payType = res.pay_type
        console.log("toPay")
        payUserOrder({
            id: res.param.id,
            pay_type: res.pay_type

        }).then((res: Result) => {
            console.log(payType)
            // 发起微信支付
            if (payType == 'wechatpay') {
                $utils.payment(res.data.payment, res.data);
            } else {
                toSuccess(res.data)
            }
        }).catch((err: ResultError) => {
            $utils.errorMsg(err);
        });
    }

    /**
       * 跳转
    */
    const toSuccess = (data: any) => {
        $utils.routeRedirectTo('/otherpages/order/result/index', data);
    }

    // 申请售后
    const _onApplyRefund = (orderGoodsId: number) => {
        $utils.routeRedirectTo('/otherpages/user/order/refund_apply', {
            order_goods_id:orderGoodsId
        });
    }

    return {
        _onCory,
        _cancelOrder,
        _toExpress,
        _onReceipt,
        _onComment,
        _onPay,
        _toPay,
        _onGoods,
        _onApplyRefund
    }

}